(function($){
    $.fn.TLSlider = function(op){
        var ops = $.extend({},$.fn.TLSlider.defaults,op);
        var cur =1;
        var ob = $(this);
        var img = ob.find('img');
        var h = ob.height();
        var w = ob.width();
        var n = img.length;
        var ele = $('ul',ob);
        var lh = ops.height==0?h:ops.height;
        var lw = ops.width==0?w:ops.width;
        ele.css({'width':w*n});
        ob.css({'overflow':'hidden','position':'relative'});
        $('li',ob).css({'float':'left','height':lh,'width':lw,'overflow':'hidden','margin':'0px','padding':'0px','list-style':'none'});
        $('ul',ob).css({'margin':'0px','padding':'0px','list-style':'none'});
        img.css({'float':'left','height':lh,'width':lw});
        
        if(ops.numnav){
           addnavdiv();
        }
        if(ops.watermark == 1){
            var mark="<span id='slider-watermark'>"+ops.mark+"</span>";
            ob.append(mark);
            addwatermarkcss();
        }
        if(ops.watermark == 2){
            var mark="<span id='slider-watermark'><img src='"+ops.mark+"'/></span>";
            ob.append(mark);    
            addwatermarkcss();
        }
        if(ops.isbg){
             var title= img.eq(0).attr('title')
             var bgdiv ="<div id='slider-title-bg'><span id='slider-img-title'>"+title+"</span></div>";
             ob.append(bgdiv);
             addtitlecss();
        }
        function andtitle(i){
            var imgtitle= img.eq(i).attr('title');
            $("#slider-img-title").html(imgtitle);
        }
        function andnavon(i){
            ob.find(".slider-num-nav-span").eq(i).css({'background-color':'#B5110F', 'color':'#B5110F'}).siblings().css({'background-color':'#FFFFFF', 'color':'#FFFFFF'});
        }
        function slide(i,on){
            ele.stop();
            var next = i==n?1:i+1;
            var l = (i-1)*w*(-1);
            cur=i;
            if(ops.isbg){
                andtitle((i-1));
            }
            if(ops.numnav){
                andnavon((i-1));
            }
            ele.animate({marginLeft:l},ops.speed);
            if(on){
                clearTimeout(timeout);
            }
            if(!on){
                timeout = setTimeout(function(){
                    slide(next,false);
                },ops.pause);
            }
        }
        //添加导航div
        function addnavdiv(){
            var nav="<div id='slider-num-nav'>";
            for(var i=0;i<n;i++){
                nav+="<span class='slider-num-nav-span'>"+(i+1)+"</span>";
            }
            nav+="</div>";
            ob.append(nav);
            addnavcss();
            ob.find('.slider-num-nav-span').hover(function(){
                    var num = $(this).html();
                    slide(num,true);
            },function(){
                    if(cur>=n){cur=1;}
                    timeout = setTimeout(function(){
                        slide((cur/1+1),false);
                    },ops.pause/4);
            });
        }
        function addnavcss(){
            $("#slider-num-nav").css({
                'max-width':'150px',
                'height':'25px',
                'position':'absolute',
                'bottom':'6px',
                'right':'20px',
                'z-index':'1200'
            });
            $(".slider-num-nav-span").css({
                'width':'10px',
                'height':'10px',
                'float':'left',
                'background-color':'#FFFFFF',
                'margin':'0 2px',
                'cursor':'pointer',
                'margin-top':'5px',
                'font-size':'2px',
                'color':'#FFFFFF',
                'overflow':'hidden'
            });
        }
        function addtitlecss(){
            $("#slider-title-bg").css({
                'width':'100%',
                'height':'40px',
                'background':'url(slider-title-bg.png) repeat-x',
                'position':'absolute',
                'bottom':'0px', 'left':'0px'
            })
            $("#slider-img-title").css({
                'width':'400px',
                'padding':'0 10px',
                'height':'30px',
                'float':'left',
                'margin-top':'9px',
                'font-size':'18px',
                'color':'red',
                'font-family':'微软雅黑',
                'font-weight':'bold'
            })
        }
        function addwatermarkcss(on){
            $("#slider-watermark").css({ 
                'position':'absolute', 
                'font-size':'18px',
                'font-weight':'bold', 
                'z-index':'999',
                'top':'10px',
                'color':'#E4F8EC', 
                'font-family':'微软雅黑'
            });
            $("#slider-watermark").css(ops.markon,'15px');
        }
        andnavon(0);
        var timeout;
        timeout = setTimeout(function(){
                slide(2,false);
            },(ops.pause/2));
    }
    //默认参数
    $.fn.TLSlider.defaults = {
        watermark:1,//是否使用水印，0不使用，1使用文字水印，2使用图片水印
        mark:'',//文字或者图片URL
        markon:'right',//水印位置左上和右上
        numnav:true,//是否显示数字导航
        isbg:true,//是否需要标题背景
        height:270,//默认图片宽度
        width:500,//默认图片高度
        pause:6000,//时间间隔
        speed:600//滑行速度
    };
})(jQuery);
